package com.farmer.reliablemessage;

import com.farmer.reliablemessage.api.IDApi;
import com.farmer.reliablemessage.mapper.MessageManagerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.sql.SQLException;

/**
 * @author tech-famer
 * @ClassName: CommonConfiguration
 * @Description: (这里用一句话描述这个类的作用)
 * @date 2021/11/1 12:43
 */
@Configuration
@EnableAsync
@EnableScheduling
@ComponentScan("com.farmer.reliablemessage")
@AutoConfigureAfter(DatasourceConfiguration.class)
public class CommonConfiguration {

    private static final Logger log = LoggerFactory.getLogger(CommonConfiguration.class);
    private static String lockID;

    public static String getLockID() {
        return lockID;
    }

    public CommonConfiguration(MessageManagerMapper messageManagerMapper, ReliableMessageProperties properties, IDApi idApi) throws SQLException {
        if (properties.isAutocreatetable()) {
            messageManagerMapper.createmessage();
            messageManagerMapper.createmessagehistory();
            messageManagerMapper.createfailedmessage();
            log.info("tables[reliablemessage, reliablemessagehistory, reliablefailedmessage, reliablemessagelock] created!");
        }
        lockID = idApi.nextID();
        log.info("local message system initial success, current terminal's lockID is {}!", lockID);
    }
}
